<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item :label="$t('holiday_group_name')" prop="vacationGroupName">
        <el-input
          v-model="queryParams.vacationGroupName"
          :placeholder="$t('please_enter', { name: $t('holiday_group_name') })"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('country_region')" prop="country">
        <el-input
          v-model="queryParams.country"
          :placeholder="$t('please_enter', { name: $t('country_region') })"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vacation:vacationGroup:add']"
          >{{ $t("add_new_holiday_group") }}</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sectionList"
      border
      :max-height="tableHeight"
    >
      <el-table-column
        :label="$t('holiday_group_name')"
        align="center"
        prop="vacationGroupName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('holiday_group_code')"
        align="center"
        prop="vacationGroupCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('country_region')"
        align="center"
        prop="country"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('remarks')"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['vacation:vacationGroup:view']"
            >{{ $t("view") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vacation:vacationGroup:edit']"
            >{{ $t("modify") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vacation:vacationGroup:delete']"
            >{{ $t("delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改假期组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="180px"
        :disabled="lookView"
      >
        <el-card class="box-card" style="margin-bottom: 20px">
          <div slot="header" class="clearfix">
            <span>{{ $t("basic_information") }}</span>
          </div>
          <el-form-item
            :label="$t('holiday_group_name')"
            label-width="100px"
            prop="vacationGroupName"
          >
            <el-input
              v-model="form.vacationGroupName"
              :placeholder="
                $t('please_enter', { name: $t('holiday_group_name') })
              "
              :show-word-limit="true"
            />
          </el-form-item>
          <el-form-item
            :label="$t('holiday_group_code')"
            label-width="100px"
            prop="vacationGroupCode"
          >
            <el-input
              v-model="form.vacationGroupCode"
              :placeholder="
                $t('please_enter', { name: $t('holiday_group_code') })
              "
              :show-word-limit="true"
            />
          </el-form-item>
          <el-form-item
            :label="$t('country_region')"
            label-width="100px"
            prop="country"
          >
            <el-input
              v-model="form.country"
              :placeholder="$t('please_enter', { name: $t('country_region') })"
              :show-word-limit="true"
            />
          </el-form-item>
          <el-form-item
            :label="$t('remarks')"
            label-width="100px"
            prop="remark"
          >
            <el-input
              v-model="form.remark"
              :rows="4"
              :show-word-limit="true"
              type="textarea"
              :placeholder="$t('please_enter', { name: $t('remarks') })"
            />
          </el-form-item>
        </el-card>
        <el-card class="box-card" style="margin-bottom: 20px">
          <div slot="header" class="clearfix">
            <span>{{ $t("holiday_group_setting") }}</span>
          </div>
          <div style="width: 94%; margin: 10px auto">
            <vacationModal
              :checkData="form.vacationList"
              :lookView="lookView"
              ref="vacationModal"
              v-if="open"
            >
            </vacationModal>
          </div>
          <el-form-item
            :label="$t('advanced_leave_request')"
            prop="isAllowAdvanceVacation"
          >
            <el-radio-group v-model="form.isAllowAdvanceVacation">
              <el-radio
                :label="parseInt(dict.value)"
                v-for="dict in dict.type.yes_or_no"
                :key="dict.value"
                >{{ dict.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item
            :label="$t('viewing_colleagues_holiday')"
            prop="isAllowViewingColleagueHolidays"
          >
            <el-radio-group v-model="form.isAllowViewingColleagueHolidays">
              <el-radio
                :label="parseInt(dict.value)"
                v-for="dict in dict.type.is_look_worker_vacation"
                :key="dict.value"
                >{{ dict.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-card>
        <el-card class="box-card" style="margin-bottom: 20px">
          <div slot="header" class="clearfix">
            <span>{{ $t("employer_configuration") }}</span>
          </div>
          <el-form-item
            :label="$t('select_employing_company')"
            prop="employerName"
          >
            <employerListSelect
              :employerlist="employerlist"
              v-model="form.employerName"
              @geteMployerList="geteMployerList"
            ></employerListSelect>
          </el-form-item>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!lookView">
        <el-button type="primary" @click="submitForm">{{
          $t("confirm")
        }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
      <div slot="footer" class="dialog-footer" v-else>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSection,
  getSection,
  delSection,
  addSection,
  updateSection,
} from "@/api/custom/vacationGroup.js";
import vacationModal from "./vacationModal.vue";
import vacationTable from "@/views/custom/vacationList/vacationTable.vue";
import employerListSelect from "./employerListSelect";
export default {
  name: "Section",
  dicts: ["yes_or_no", "vacation_type", "is_look_worker_vacation"],
  components: {
    vacationModal,
    vacationTable,
    employerListSelect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 假期组表格数据
      sectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        country: null,
        sectionName: null,
        countryRegion: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vacationGroupName: [
          {
            required: true,
            message: this.$t("cannot_empty", {
              name: this.$t("holiday_group_name"),
            }),
            trigger: "blur",
          },
        ],
        vacationGroupCode: [
          {
            required: true,
            message: this.$t("cannot_empty", {
              name: this.$t("holiday_group_code"),
            }),
            trigger: "blur",
          },
        ],
        country: [
          {
            required: true,
            message: this.$t("cannot_empty", {
              name: this.$t("country_region"),
            }),
            trigger: "blur",
          },
        ],
      },
      lookView: false,
      employerlist: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询假期组列表 */
    getList() {
      this.loading = true;
      listSection(this.queryParams).then((response) => {
        this.sectionList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sectionName: null,
        sectionCode: null,
        countryRegion: null,
        remark: null,
        anticipatedVacation: null,
        allowToView: null,
        employerName: null,
        vacationList: null,
        vacationList: null,
      };
      this.lookView = false;
      this.resetForm("form");
    },
    geteMployerList(val) {
      console.log(val);
      this.form.employerList = val;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.employerlist = [];
      this.open = true;
      this.title = this.$t("add");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSection(id).then((response) => {
        this.form = response.data;
        if (this.form.vacationList.length > 0) {
          let data = [];
          this.form.vacationList.forEach((element) => {
            data.push(element.vacation);
          });
          this.form.vacationList = data;
        }
        this.open = true;
        this.title = this.$t("modify");
      });
    },
    //查看操作
    handleView(row) {
      this.reset();
      const id = row.id;
      getSection(id).then((response) => {
        this.form = response.data;
        if (this.form.vacationList.length > 0) {
          let data = [];
          this.form.vacationList.forEach((element) => {
            data.push(element.vacation);
          });
          this.form.vacationList = data;
        }
        if (this.form.employerList.length > 0) {
          let list = [];
          this.form.employerList.forEach((element) => {
            list.push(element.employer);
          });
          this.employerlist = list;
        }
        this.open = true;
        this.title = this.$t("view");
        this.lookView = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let checkList = this.$refs.vacationModal.checkList;
          if (checkList.length) {
            let data = [];
            checkList.forEach((element) => {
              data.push({ vacationId: element.id });
            });
            this.form.vacationList = data;
          }
          if (this.form.id != null) {
            updateSection(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          } else {
            addSection(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delSection(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "section/section/export",
        {
          ...this.queryParams,
        },
        `vacationGroup_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
